import React, {useEffect} from "react";
import {getParameter} from "@/common/utils";
import {useNavigate} from "react-router-dom";
import http from "@/common/http";
import {LOGIN_INFO} from "@/common/constant";

const CallBack: React.FC<any> = (props) => {

  const navigate = useNavigate();
  const code = getParameter('code')
  console.log('code:', code)

  if (code) {
    http({
      method: 'POST',
      url: '/auth/oauth/token',
      headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'},
      data: `grant_type=authorization_code&code=${code}`,
      auth: {username: 'clientapp', password: 'abcd'}
    }).then((res: any) => {
      console.log(res)
      if (res.code === 0) {
        localStorage.setItem(LOGIN_INFO, JSON.stringify(res.data))
        navigate("/", {replace: true});
      }
    })
  }

  return (
    <div/>
  )
}

export default CallBack
